import util from "$util"

const state = {
  active: "ScanPage", // 当前展示的tab，ScanPage是扫描单据tab，Barcode是扫电池tab
  showBarcode: false, // 页面底部是否显示扫电池tab
  canScan: true, // 扫电池页面是否允许扫电池
  info: null, // 表格上面的数据
  body: {
    list: [], // 表格的数据
  },

  index: 0, // 点击的行的行号
  detailId: "", // 点击的行的唯一id
  codeMap: {}, // 已扫描的电池码，用来去重
  barcodeBody: {
    list: [], // 点击的行对应的已扫描的电池数据
  },
  statusMap: {}, // 电池码　->  合法/不合法，用来显示不同的颜色

  /* 缓存每一列扫的电池列表
  格式
  {
    barcodeList: [],
    statusMap: {},
  } */
  partnoAndList: {},
}

const getters = {
  // 减去底部tab区域占用的高度
  minusBottomHeight(state) {
    if (state.showBarcode) {
      return 35
    } else {
      return 0
    }
  },
}

const mutations = {
  // 切换tab
  selectTab(state, { tab }) {
    if (state.active === tab || tab === "BarcodeInfo") {
      return
    }
    // 切换tab时要重新给点击行的已扫电池列表赋值
    state.partnoAndList[state.detailId].barcodeList = state.barcodeBody.list
    state.partnoAndList[state.detailId].statusMap = state.statusMap
    state.active = tab
  },
  // 切换到扫电池界面
  switchToBarcode(state, { index }) {
    state.index = index
    state.detailId = state.body.list[index].OutputDetailId
    switch (state.info.ConfirmFlag) {
      case 1:
      case 2:
      case 3:
        state.canScan = false
        break
      default:
        state.canScan = true
        break
    }

    if (!state.partnoAndList[state.detailId]) {
      state.partnoAndList[state.detailId] = {}
    }
    let m = state.partnoAndList[state.detailId]
    state.barcodeBody.list = m.barcodeList || []
    state.statusMap = m.statusMap || {}

    let codeMap = {}
    for (let item of state.barcodeBody.list) {
      codeMap[item.BarcodeData] = true
    }
    state.codeMap = codeMap

    if (state.active !== "BarcodeInfo") {
      state.active = "BarcodeInfo"
    }
  },
  // 给info赋值
  setInfo(state, { info }) {
    state.info = info
  },
  // 给body赋值
  setBody(state, { body }) {
    state.body = body
  },
  // 设置表格列的样式
  setCellStyle(state, { styleMap }) {
    state.body.styleMap = styleMap
  },
  // 扫电池的列表里添加一行数据
  addBarcode(state, { vue, data, status }) {
    if (status === util.STATUS_ERROR) {
      util.Alert(`${data.BarcodeData}不在此仓库`, "错误")
      return
    }
    let item = state.body.list[state.index]
    if (item._realNum === item.RequiredQty) {
      util.Alert("电池扫描数量已足够", "错误")
      return
    }

    item._realNum = item._realNum ? item._realNum + 1 : 1
    vue.$set(state.body.list, state.index, item)

    data.DetailId = state.detailId
    state.barcodeBody.list.push(data)
    state.statusMap[data.BarcodeData] = status
    state.codeMap[data.BarcodeData] = true
  },
  // 扫电池的列表里删除一行数据
  deleteBarcode(state, { vue, barcode }) {
    let bodyIndex = -1
    for (let [i, item] of state.barcodeBody.list.entries()) {
      if (item.BarcodeData === barcode) {
        bodyIndex = i
        break
      }
    }
    if (bodyIndex >= 0) {
      state.barcodeBody.list.splice(bodyIndex, 1)
      let item = state.body.list[state.index]
      if (state.statusMap[barcode] === util.STATUS_OK) {
        item._realNum -= 1
        vue.$set(state.body.list, state.index, item)
      }
    }
    state.codeMap[barcode] = false
  },
  // 页面底部显示扫电池tab
  showBarcode(state) {
    state.showBarcode = true
  },
  // 清除电池缓存数据
  clearBatteryCache(state) {
    state.partnoAndList = {}
  },
  // 存储电池缓存数据
  setBatteryCache(state, { list }) {
    state.partnoAndList[state.detailId].barcodeList = list
    state.barcodeBody.list = list
  },
  // 重置所有页面的状态
  reset(state) {
    state.index = 0
    state.detailId = ""
    state.codeMap = {}

    state.info = null
    state.body = {
      list: [],
    }
    state.barcodeBody = {
      list: [],
    }

    state.partnoAndList = {}
    state.statusMap = {}
    state.showBarcode = false
    state.active = "ScanPage"
  },
}

export default {
  state,
  getters,
  mutations,
  namespaced: true,
}
